<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <el-breadcrumb :separator-icon="ArrowRight" class="bread">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="'/category/'+goods.categories[0].id">{{goods.categories[0].name}}</el-breadcrumb-item>
        <el-breadcrumb-item :to="'/category/sub/'+goods.categories[1].id">{{goods.categories[1].name}}</el-breadcrumb-item>
        <el-breadcrumb-item>{{goods.name}}</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
          <!-- 名字区组件 -->
          <GoodsName :goods="goods"/>
          <!-- 规格组件 -->
          <GoodsSku :goods="goods" @change="changeSku" />
          <Numbox label="数量" v-model="num" :max="goods.inventory"/>
          <MyButton type="primary" style="margin-top:20px;" @click="insertCart()">加入购物车</MyButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs :goods="goods" />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import GoodsRelevant from './goods-relevant'
import GoodsImage from './goods-image'
import GoodsSales from './goods-sales'
import GoodsName from './goods-name'
import GoodsSku from './goods-sku'
import GoodsTabs from './goods-tabs'
import GoodsHot from './goods-hot'
import GoodsWarn from './goods-warn'
import { ArrowRight } from '@element-plus/icons-vue'
import { useGoods } from '@/hooks/product'
import Numbox from '@/components/numbox.vue'
import MyButton from '@/components/my-button'
import { ref, provide } from 'vue'
import { useStore } from 'vuex'
import Swal from 'sweetalert2'
  const num = ref(1)
  const goods = useGoods()
  const changeSku = (sku) => {
    if (sku.skuId) {
      goods.value.price = sku.price
      goods.value.oldPrice = sku.oldPrice
      goods.value.inventory = sku.inventory
      currSku.value = sku
    }else {
      currSku.value = null
    }
  }
  const currSku = ref(null)
  const store = useStore()
  const insertCart = () => {
    if (!currSku.value) {
      Swal.fire({
        icon: 'warning',
        text: '请选择商品规格',
        title: '提示',
        showConfirmButton: false,
        timer: 1500
      })
      return
    }
    if (num.value > goods.inventory) {
      Swal.fire({
        icon: 'warning',
        text: '库存不足',
        title: '提示',
        showConfirmButton: false,
        timer: 1500
      })
      return
    }
    store.dispatch('cart/insertCart', {
      id: goods.value.id,
      skuId: currSku.value.skuId,
      name: goods.value.name,
      picture: goods.value.mainPictures[0],
      price: currSku.value.price,
      nowPrice: currSku.value.price,
      count: num.value,
      attrsText: currSku.value.specsText,
      selected: true,
      isEffective: true,
      stock: currSku.value.inventory
    }).then(() => {
      Swal.fire({
        icon: 'success',
        text: '商品加入购物车成功',
        title: '成功',
        showConfirmButton: false,
        timer: 1500
      })
    })
  }
  provide('goods', goods)
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.bread{
    display: flex;
    padding: 25px 10px;
    // ul li:last-child {}
    // 先找到父元素，找到所有的子元素，找到最后一个，判断是不是LI，是就是选中，不是就是无效选择器
    // ul li:last-of-type {}
    // 先找到父元素，找到所有的类型为li的元素，选中最后一个
    &-item {
      a {
        color: #666;
        transition: all .4s;
        &:hover {
          color: @mainColor;
        }
      }
    }
    i {
      font-size: 12px;
      margin-left: 5px;
      margin-right: 5px;
      line-height: 22px;
      // 样式的方式，不合理
      // &:last-child {
      //   display: none;
      // }
    }
  }
</style>